/*
 * $Id: AnalogClock.java 107 2009-05-05 16:59:15Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.deltagui.widget;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;



/**
 * AnalogClock.
 * 
 * Inspired by the clock component of thinlet.
 * 
 * @author Andreas Meyer
 * @version $Revision: 107 $
 */
public class AnalogClock extends Control {
	
	/** The refresh timer. */
	private Timer timer;
	
	
	/**
	 * Construct the clock.
	 * 
	 */
	public AnalogClock() {
		
		timer = new Timer("DeltaGUI-AnalogClock-Timer");
		
		setBackgroundColor(null);
		setPreferredSize(96, 96);
	}

	/**
	 * @see deltadesk.gui.deltagui.widget.Control#onActivate()
	 */
	@Override
	protected void onActivate() {
		
		final TimerTask tt = new TimerTask() {

			@Override
			public void run() {
				setUnrendered();
				
			}
		};
		timer.scheduleAtFixedRate(tt, 0, 1000);
	}

	/**
	 * @see deltadesk.gui.deltagui.widget.Control#onDeactivate()
	 */
	@Override
	protected void onDeactivate() {
		timer.cancel();
	}

	/**
	 * @see deltadesk.gui.deltagui.widget.Control#renderControl(java.awt.Graphics2D)
	 */
	@Override
	public void renderControl(final Graphics2D g2) {
		
		final int w = Math.min(getWidth(), getHeight());
		
		g2.setColor(Color.lightGray);
		g2.fillOval(0, 0, w, w);
		g2.setColor(Color.white);
		g2.fillOval(2, 2, w-4, w-4);

//		g2.setColor(Color.lightGray);
//		for (int i = 0; i < 12; i++) {
//			double angle = 2 * Math.PI * i / 12, ix = Math.sin(angle), iy = Math.cos(angle);
//			g2.drawLine(48 + (int) (42 * ix), 48 - (int) (42 * iy), 48 + (int) (46 * ix), 48 - (int) (46 * iy));
//		}

		Calendar calendar = Calendar.getInstance();
		double hour   = calendar.get(Calendar.HOUR),
		       minute = calendar.get(Calendar.MINUTE),
			   second = calendar.get(Calendar.SECOND);
		
		g2.setColor(Color.gray);
		paintLine(g2, second / 60, w/2-w/12, w);
		Stroke stroke = g2.getStroke();
		g2.setStroke(new BasicStroke(2));
		g2.setColor(Color.darkGray);
		paintLine(g2, (minute + second / 60) / 60, w/2-w/12, w);
		g2.setColor(Color.black);
		paintLine(g2, (hour + minute / 60) / 12, w/2-w/5, w);
		g2.setStroke(stroke);

		g2.setColor(Color.gray);
		g2.fillOval(w/2-4, w/2-4, 8, 8);
	}

	/**
	 * Draw a clock needle.
	 * 
	 * @param g
	 * @param value
	 * @param length
	 * @param w
	 */
	private void paintLine(final Graphics2D g,
			               final double value,
			               final int length,
			               final int w) {
		double angle = value * 2 * Math.PI;
		g.drawLine(w/2, w/2, 
				  w/2 + (int) (length * Math.sin(angle)),
				  w/2 - (int) (length * Math.cos(angle)));
	}

}
